package com.teatrace.action;

import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.RequestAware;

import com.opensymphony.xwork2.ActionSupport;
import com.teatrace.commons.util.PageHelper;
import com.teatrace.model.base.Device;
import com.teatrace.model.base.Employee;
import com.teatrace.model.base.Field;
import com.teatrace.model.base.Vehicle;
import com.teatrace.service.ServiceContext;
import com.teatrace.service.basic.BasicService;

public class BasicAction extends ActionSupport implements RequestAware {
	
	private static final Logger log = Logger.getLogger(BasicAction.class);
	
	private Map<String, Object> req;
	private BasicService service;
	
	private Employee emp;
	private Field f;
	private Device device;
	private Vehicle vehicle;
	
	
	private int page = 1;
	
	public BasicAction() {
		service = ServiceContext.getService(BasicService.class);
	}

	public String getEmpDetails() {
		try {
			req.put("emp", service.getById(Employee.class, emp.getId()));
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	
	
	public String toUpdateEmp() {
		return getEmpDetails();
	}
	
	public String updateEmp() {
		if(emp.getBirthday() == null || emp.getBirthday().equals("")) {
			emp.setBirthday(null);
		}
		try {
			service.update(emp);
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String deleteEmp() {
		try {
			service.deleteById(Employee.class, emp.getId());
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String addEmp() {
		if(emp.getBirthday() == null || emp.getBirthday().equals("")) {
			emp.setBirthday(null);
		}
		try {
			service.add(emp);
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String listEmp() {
		try {
			req.put("empList", service.listByPage(Employee.class, page));
			req.put("total", PageHelper.getTotalPage(service.count(Employee.class)));
			req.put("page", page);
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}

	
	public String addField() {
		try {
			//System.out.println(BeanUtils.describe(f));
			service.add(f);
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String toUpdateField() {
		return getFieldDetails();
	}
	
	public String updateField() {
		
		try {
			service.update(f);
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String getFieldDetails() {
		try {
			req.put("field", service.getById(Field.class, f.getId()));
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String deleteField() {
		try {
			service.deleteById(Field.class, f.getId());
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	
	public String listField() {
		try {
			req.put("fieldList", service.listByPage(Field.class, page));
			req.put("total", PageHelper.getTotalPage(service.count(Field.class)));
			req.put("page", page);
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	
	/******************************/
	public String addDevice() {
		if(device.getBuyTime() == null || device.getBuyTime().equals("")) {
			device.setBuyTime(null);
		}
		try {
			service.add(device);
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String toUpdateDevice() {
		return getDeviceDetails();
	}
	
	public String updateDevice() {
		if(device.getBuyTime() == null || device.getBuyTime().equals("")) {
			device.setBuyTime(null);
		}
		try {
			service.update(device);
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String getDeviceDetails() {
		try {
			req.put("device", service.getById(Device.class, device.getId()));
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String deleteDevice() {
		try {
			service.deleteById(Device.class, device.getId());
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	
	public String listDevice() {
		try {
			req.put("deviceList", service.listByPage(Device.class, page));
			req.put("total", PageHelper.getTotalPage(service.count(Device.class)));
			req.put("page", page);
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	/*****************************************/
	
	
	public String addVehicle() {
		
		try {
			service.add(vehicle);
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String toUpdateVehicle() {
		return getVehicleDetails();
	}
	
	public String updateVehicle() {
		
		try {
			service.update(vehicle);
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String getVehicleDetails() {
		try {
			req.put("vehicle", service.getById(Vehicle.class, vehicle.getId()));
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String deleteVehicle() {
		try {
			service.deleteById(Vehicle.class, vehicle.getId());
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	
	public String listVehicle() {
		try {
			req.put("vehicleList", service.listByPage(Vehicle.class, page));
			req.put("total", PageHelper.getTotalPage(service.count(Vehicle.class)));
			req.put("page", page);
		} catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return SUCCESS;
	}
	
	/************  getters and setters ***********/
	
	public Employee getEmp() {
		return emp;
	}

	public void setEmp(Employee emp) {
		this.emp = emp;
	}

	public void setRequest(Map<String, Object> req) {
		this.req = req;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public Field getF() {
		return f;
	}

	public void setF(Field f) {
		this.f = f;
	}

	public Device getDevice() {
		return device;
	}

	public void setDevice(Device device) {
		this.device = device;
	}

	public Vehicle getVehicle() {
		return vehicle;
	}

	public void setVehicle(Vehicle vehicle) {
		this.vehicle = vehicle;
	}
	
	
}
